"""
Case Type   : 逻辑复制
Case Name   : 使用JDBC创建逻辑复制槽,且复制槽名称大于64个字符
Create At   : 2021/01/15
Owner       : opentestcase015
Description :
    1.创建名称大于64个字符的逻辑复制槽
Expect      :
    1.创建名称大于64个字符的逻辑复制槽失败
History     :modified by zopentestcase005,2022.09.13，研发代码变更，含有\n符号的逻辑复制槽,
             报错提示为含危险字符(没有换行的情况下，超过64个字符会截取)
"""

import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

LOG = Logger()


class LogicalReplication(unittest.TestCase):
    def setUp(self):
        LOG.info('----------------this is setup-----------------------')
        LOG.info(
            '---Opengauss_Function_Logical_Replication_Case0010开始执行-----')
        self.comsh = CommonSH('dbuser')
        self.constant = Constant()
        self.common = Common()
        self.default_value = self.common.show_param('wal_level')

    def test_logical_replication(self):
        LOG.info(
            '----------------设置wal_level = logical----------------------')
        msg = self.comsh.execut_db_sql('show wal_level;')
        LOG.info(msg)
        self.parameter_values = msg.splitlines()[-2].strip()
        msg = self.comsh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                       'wal_level=logical')
        LOG.info(msg)
        LOG.info('---------------------重启数据库--------------------')
        self.comsh.restart_db_cluster()
        status = self.comsh.get_db_cluster_status()
        self.assertTrue("Normal" in status or 'Degraded' in status)
        msg1 = self.comsh.execut_db_sql('show wal_level;')
        LOG.info(msg1)
        self.new_value = msg1.splitlines()[-2].strip()
        self.assertIn('logical', self.new_value)

        LOG.info(
            '---------------------创建逻辑复制槽-------------------------')
        msg = self.comsh.execut_db_sql('''
            SELECT * FROM pg_create_logical_replication_slot
            ('slot1slot1slot1slot1slot1slot1slot1slot1slot1slot1
            slot1slot1slot1slotslot', 'mppdb_decoding');''')
        LOG.info(msg)
        self.assertIn('contains dangerous character', msg)

    def tearDown(self):
        LOG.info('----------------this is tearDown-----------------------')
        msg = self.comsh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                       f"wal_level={self.default_value}")
        LOG.info(msg)
        self.comsh.restart_db_cluster()
        status = self.comsh.get_db_cluster_status()
        self.assertTrue("Normal" in status or 'Degraded' in status)
        LOG.info(
            '-----Opengauss_Function_Logical_Replication_Case0010执行完成---')
